package com.common.exception;

import com.common.response.ResponseVO;
import com.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.util.Map;

/**
 * 全局异常处理器
 */

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

	// 自定义异常捕捉处理方法
	@ExceptionHandler(RRException.class)
	public R RRExceptionHandle(RRException e) {
		log.error(e.getMsg());
		return R.error(e.getCode(), e.getMsg());
	}

	// 404异常捕捉处理方法
	@ExceptionHandler(NoHandlerFoundException.class)
	public R NoHandlerFoundException(NoHandlerFoundException e) {
		log.error(e.toString());
		return R.error(404, "未找到资源");
	}

	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public R handleException(HttpRequestMethodNotSupportedException e) {
		log.error(e.getMessage(), e);
		return R.error("请求类型错误");
	}

	@ExceptionHandler(Exception.class)
	public R handleException(Exception e) {
		log.error(e.getMessage(), e);
		return R.error("系统错误");
	}
}
